package com.kaishengit.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.entity.User;
import com.kaishengit.service.UserService;

public class AddPerson extends HttpServlet {


	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		UserService userService = new UserService();
		
		User user1 = userService.findByUsername(username);
		if(user1==null) {
		
			UUID uuid = UUID.randomUUID();
			
			User user = new User();
			
			user.setId(uuid.toString());
			user.setUsername(username);
			user.setPassword(password);
			
			userService.save(user);
			
			response.sendRedirect("addpersonview.jspx?state=10002");
		}else {
			response.sendRedirect("addpersonview.jspx?state=10001");
		}	

	
	}

	
	public void init() throws ServletException {
		// Put your code here
	}
	
	

}
